package com.wei.wiki.controller;

/*
 * @author : ShunWei
 * @date : 2022/1/27 17:20
 * @Description:
 */

import com.wei.wiki.req.CategoryQueryReq;
import com.wei.wiki.req.CategorySaveReq;
import com.wei.wiki.resp.CategoryQueryResp;
import com.wei.wiki.resp.CommonResp;
import com.wei.wiki.resp.PageResp;
import com.wei.wiki.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category")
@Api(tags = "CategoryController", description = "一二级菜单控制类")
public class CategoryController {
    private CategoryService categoryService;

    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("/all")
    @ApiOperation("一二级菜单列表")
    public CommonResp getAllCategoryList() {
        CommonResp<List<CategoryQueryResp>> resp = new CommonResp<>();
        List<CategoryQueryResp> categoryList = categoryService.all();
        resp.setSuccess(true);
        resp.setContent(categoryList);
        resp.setMessage("success");
        return resp;
    }

    @GetMapping("/list")
    @ApiOperation("一二级菜单列表 分页")
    public CommonResp getCategoryList(@Valid CategoryQueryReq req) {
        CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();
        PageResp<CategoryQueryResp> categoryList = categoryService.categoryList(req);
        resp.setSuccess(true);
        resp.setContent(categoryList);
        resp.setMessage("success");
        return resp;
    }

    @PostMapping("/save")
    @ApiOperation("新增、修改一二级菜单")
    public CommonResp save(@RequestBody @ApiParam("一二级菜单对象") CategorySaveReq req) {
        CommonResp resp = new CommonResp<>();
        categoryService.save(req);
        resp.setSuccess(true);
        resp.setMessage("success");
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据id删除一二级菜单")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        categoryService.delete(id);
        return resp;
    }
}
